<template>
  <div class="chooseaddr">
    <!-- 地址选择对话框 -->
    <el-dialog
      title="联系地址"
      :visible.sync="chooseAddr"
      width="30%">
      <el-form :model="addrForm" ref="addrForm" size="medium" label-width="80px">
        <el-form-item label="地址:">
          <v-distpicker :province="addrForm.province" :city="addrForm.city" :area="addrForm.area" @selected="selected"></v-distpicker>
        </el-form-item>
        <el-form-item label="详细地址:" prop="address_detail">
          <el-input v-model="addrForm.address_detail" placeholder="地址详情,如4号楼2单元"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="chooseAddr = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import VDistpicker from 'v-distpicker'
  export default {
    name: 'ChooseAddr',
    components: { VDistpicker },
    props:{},
    data() {
      return {
        chooseAddr: false,
        addrForm: {
          province: '',
          city: '',
          area: '',
          address_detail: ''
        }
      }
    },

    methods: {
      //打开弹窗
      open() {
        this.chooseAddr = true;
      },

      // 省市区选择最后一项触发
      selected(data) {
      // 对象的遍历,添加到另一对象中
        for (var obj in data) {
          this.addrForm[obj] = data[obj].value
        }
      },

      //保存
      save () {
        this.$refs['addrForm'].validate(valid => {
          if (valid) {
            console.log('form',this.addrForm)
            this.$emit('valAddr',this.addrForm); //传addrForm中的值到父组件
            this.chooseAddr = false;
            this.$message.success('保存成功')
          } else {
            this.$message.error('表单验证失败!')
          }
        })
      }
    }
  }
</script>

<style>
</style>
